import { getExamType, getExamSubject, getQuestionType, submitQuestion, insertQuestions, examQuestions, questionsAll } from '@/services';
import { Effect, Reducer } from 'umi';
import { IQuestionType, IExamType, ISubject, IExamQuestions, InewType } from '@/utils/interface'
export interface addQuestions {
  data: { [key: string]: any },
  questionTypes: IQuestionType[],
  subjects: ISubject[],
  examTypes: IExamType[]
  examQuestions: IExamQuestions[],
  newTypes: InewType[]

}
export interface addQT {
  namespace: 'addQuestions';
  state: addQuestions;
  effects: {
    getQT: Effect
    getQuestionType: Effect;
    getExamSubject: Effect;
    getExamType: Effect;
    submitQuestion: Effect;
    insertQuestionsType: Effect;
    getExamQuestions: Effect;
    newType: Effect
  };
  reducers: {
    save: Reducer<addQuestions>;
  };
}
const UserModel: addQT = {
  namespace: 'addQuestions',
  state: {
    data: [],
    questionTypes: [],
    subjects: [],
    examTypes: [],
    examQuestions: [],
    newTypes: []
  },
  effects: {
    *getQT({ payload }, { call, put }) {
      let result = yield call(getQuestionType);
      yield put({
        type: 'save',
        payload: { data: result.data }
      })
    },

    *getQuestionType({ payload }, { call, put }) {
      let result = yield call(getQuestionType);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { questionTypes: result.data }
        })
      }
    },


    *getExamSubject({ payload }, { call, put }) {
      let result = yield call(getExamSubject);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { subjects: result.data }
        })
      }
    },


    *getExamType({ payload }, { call, put }) {
      let result = yield call(getExamType);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { examTypes: result.data }
        })
      }
    },

    *insertQuestionsType({ payload }, { call, put }) {
      let result = yield call(insertQuestions, payload);
      return result
    },

    *submitQuestion({ payload }, { call, put }) {
      let result = yield call(submitQuestion, payload);
      return result;
    },


    *getExamQuestions({ }, { call, put }) {
      let result = yield call(examQuestions);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { examQuestions: result.data }
        })
      }
    },
    *newType({ payload }, { call, put }) {
      let result = yield call(questionsAll);
      if (result.data) {
        yield put({
          type: 'save',
          payload: { newTypes: result.data }
        })
      }
    }





  },


  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default UserModel;